<template>
  <div class="wrap">

    <!--<div cgoods_itemlass="fun-tab-title">商品信息</div>-->
    <div class="card line15 bgwhite">
      <div class="card-item">
        <div class="cover" :style="{backgroundImage:'url('+goods.goods_logo+')'}"></div>
        <div class="info padding10-r">
          <div class="title line10">{{goods.goods_title}}</div>
          <div class="specs line10 graytext2 font14"><span
              style="text-decoration:line-through;">￥{{goods.market_price}}</span> <span class="price padding10-c">{{goods.points_num}}</span>
          </div>
          <div class="numbox padding4-r">

            <fun-icons @click="minus" name="iconxingzhuang" size="22" :color="num>1?'#002aad':'#999'"/>
            <span class="padding10-c">{{num}}</span>
            <fun-icons @click="plus" name="iconicon_plus" size="22"
                       :color="(num<=goods.package_stock)?'#002aad':'#999'"/>
          </div>
        </div>
      </div>
    </div>

    <!--<div class="fun-tab-title">收货地址</div>-->
    <div class="address bgwhite line15" @click="chooseAddressFunc" v-if="goods.type=='goods'">
      <div v-if="address.tel" class="padding15">
        <div class="flex-between flex line10">
          <div>{{address.name}}</div>
          <div>{{address.tel}}</div>
        </div>
        <div class="flex" style="align-items: center">
          <fun-icons size="20" color="#81ce69" name="icondingwei"/>
          <div class="padding10-c">{{address.province+' '+address.city+' '+address.county+' '+address.address}}</div>
        </div>
      </div>
      <div v-else class="padding15 flex flex-between">
        <span class="graytext font14"> 选择微信收货地址</span>
        <fun-icons size="20" color="#002aad" name="icondingwei"/>
      </div>

    </div>

    <div class="action">
      <div class="total_money">所需积分<span class="price">{{get_total_money()}}</span></div>
      <button class="btn" @click="buyFn" type="primary">立即兑换</button>
    </div>

    <div class="space" style="height:50px"></div>
    <div class="safearea"></div>
  </div>
</template>
<script>

import { goods, pointsOrder } from '@/api'
import { emptyObject } from '@/common/helper'
import Storage from '@/common/Storage'
import { pageMixin } from '@/mixins'

export default {
  mixins: [pageMixin],
  data () {
    return {
      num: 1,
      goods: {},
      goods_id: null,
      address: {
        name: null,
        tel: null,
        province: null,
        city: null,
        county: null,
        address: null,
        postal_code: null
      }
    }
  },
  methods: {
    chooseAddressFunc () {
      var self = this
      uni.chooseAddress({
        success (res) {
          self.address.name = res.userName
          self.address.tel = res.telNumber
          self.address.postal_code = res.postalCode
          self.address.province = res.provinceName
          self.address.city = res.cityName
          self.address.county = res.countyName
          self.address.address = res.detailInfo

          Storage.set('address', self.address)
        }
      })
    },
    minus () {
      if (this.num > 1) {
        this.num--
      } else {
        toast('最低1个起购', 'none')
      }
    },
    plus () {
      if (this.num >= this.goods.package_stock) {
        toast('库存不足', 'none')
        return
      }

      this.num++
    },

    get_total_money () {
      // if(!this.goods.sku){
      //     return 0;
      // }
      return this.goods.points_num * this.num
    },
    async buyFn () {
      const _self = this
      const postData = {
        num: this.num,
        points_id: this.goods.id
      }

      if (this.goods.type == 'goods') {
        postData.address = JSON.stringify(_self.address)
      }

      // 如果有值为false则不提交
      if (!emptyObject(postData, 1)) return
      await pointsOrder(postData).then(res => {
        toast('兑换成功')
        setTimeout(() => {
          uni.navigateTo({
            url: '/pages/points/index'
          })
        }, 1000)
      }, err => {
      }).catch(error => {

      })

      // if (!order_no) return;
      // unifyPay({order_no: order_no, channel: 'wx_mini', body: '购买商品'+this.goods.title}).then(res => {
      //     let payConf = JSON.parse(res.data);
      //     // 仅作为示例，非真实参数信息。
      //     uni.requestPayment({
      //         provider: 'wxpay',
      //         ...payConf,
      //         success: function (res) {
      //             console.log('success:' + JSON.stringify(res));
      //
      //             toast('购买成功')
      //             uni.navigateTo({
      //                 url: '/pages/user/index'
      //             })
      //
      //         },
      //         fail: function (err) {
      //             // error('购买失败')
      //             console.log('fail:' + JSON.stringify(err));
      //         }
      //     });
      //
      // }).catch(error => {
      // })
    }
  },
  onLoad (options) {
    this.goods = Storage.get('temp_points_info')

    if (Storage.get('address')) {
      this.address = Storage.get('address')
    }
    // this.goods_id = options.goods_id
    // this.loadGoods()
  }
}
</script>
<style lang="scss" scoped>
  .card {
    .card-item {
      display: flex;
      align-items: center;

      .cover {
        width: 140rpx;
        height: 140rpx;
        margin: 30rpx;
        border-radius: 6px;
        @include cover-img(cover, 0, #f2f2f2)
      }

      .info {
        flex: 1;

        .numbox {
          display: flex;
        }
      }
    }
  }

  .action {
    position: fixed;
    bottom: constant(safe-area-inset-bottom);
    bottom: env(safe-area-inset-bottom);
    z-index: 2;
    width: 750rpx;
    display: flex;
    align-items: center;
    background: white;

    .total_money {
      padding: 0 20px;
    }

    .btn {
      flex: 1;
      border-radius: 0;
    }
  }
</style>
